<?php

class MenuController extends FController
{
    public function __construct($id,$module=null) {
        parent::__construct($id,$module);
        $this->menu=array(
            array('label'=>t('Menu Manager','menu'), 'url'=>array('/menu/admin'),'linkOptions'=>array('class'=>'btn')),
            array('label'=>t('Add New Menu','menu'), 'url'=>array('/menu/create'),'linkOptions'=>array('class'=>'btn')),
        );
    }

    public function actions() {
        return array(
            'autocomplete' => array(
                'class'=>'common.components.actions.AutoComplete',
                'model_class'=>'Menu',
                'field'=>'name',
            )
        );
    }
	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */

	public function actionCreate()
	{
		$model=new Menu;

		$this->performAjaxValidation($model);

		if(isset($_POST['Menu']))
		{
			$model->attributes=$_POST['Menu'];
			if($model->save())
                user()->setFlash('success',t('<span class="label label-success">Success</span>  {name} has been created successfully','menu',array('{name}'=>$model->name)));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Menu']))
		{
			$model->attributes=$_POST['Menu'];
			if($model->save())
                user()->setFlash('success',t('<span class="label label-success">Success</span>  {name} has been updated successfully','menu',array('{name}'=>$model->name)));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Menu('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Menu']))
			$model->attributes=$_GET['Menu'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
    /*
     * Managers all items
     * */
    public function actionAdminItem() {
        $request =  Yii::app()->request;
    }
    /*
     * Create Menu Item
     * */
    public function actionCreateItem(){
        $model = new MenuItem();
        $this->performAjaxValidation($model);
        if(isset($_POST['MenuItem'])) {
            $model->attributes = $_POST['MenuItem'];
            if($model->save()) {
                user()->setFlash('success',t('<span class="label label-success">Success</span>  {name} has been created successfully','menu',array('{name}'=>$model->label)));
            }
        }
        $this->render('create_item',array(
            'model'=>$model
            )
        );

    }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Menu::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && ($_POST['ajax']==='menu-form' || $_POST['ajax']==='menu-item-form'))
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
